#include "ds/task_queue.h"

TaskQueue_t *createTaskQueue()
{
    return (TaskQueue_t *)calloc(1, sizeof(TaskQueue_t));
}

void destroyTaskQueue(TaskQueue_t *queue)
{
    Task_t *curr = queue->front;
    while (curr != NULL)
    {
        Task_t *tmp = curr->next;
        free(curr);
        curr = tmp;
    }
    free(queue);
}


int enqueue(TaskQueue_t *queue, Task_t *task)
{
    // 判断队列为空或非空
    if (queue->size == 0)
    {
        queue->front = task;
        queue->rear = task;
    }
    else
    {
        queue->rear->next = task;
        queue->rear = task;
    }

    queue->size++;

    return 0;
}

Task_t *dequeue(TaskQueue_t *queue)
{
    if (queue->front == NULL)
    {
        return NULL;
    }

    Task_t *task = queue->front;
    queue->front = task->next;

    if (queue->size == 1)
    {
        queue->rear = NULL;
    }
    queue->size--;

    return task;
}